<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */
 
function listFiles($dir){
	$ar = array();
    if ($handle = opendir($dir)) {
	    while (false !== ($file = readdir($handle))) {
	        if ($file != "." && $file != "..") {
	        	$fname = addSlash($dir).$file;
	            if(is_dir(addSlash($dir).$file)){
	                $ar = array_merge($ar,listFiles($fname));
	            } else {
	            	array_push($ar,$fname);
	            }
	        }
	    }
    	closedir($handle);
	}
	return $ar;
}
 
function addSlash($d) {
	if (substr($d,strlen($d)-1)!="/") {
		$d .= "/";
	}
	return $d;	
}

function deltree($f) {
	$deleted=0;
    if (is_dir($f)) {
    	/*
        foreach(glob($f.'/*') as $sf) {
            if (is_dir($sf) && !is_link($sf)) {
                $deleted += deltree($sf);
            } else {
                @unlink($sf);
                $deleted+=1;
            }
        }*/
        foreach(scandir($f) as $sf) {
        	if ($sf!="." and $sf!="..") {
        		$sfd = $f."/".$sf;
	            if (is_dir($sfd) && !is_link($sfd)) {
	                $deleted += deltree($sfd);
	            } else {
	                @unlink($sfd);
	                $deleted+=1;
	            }
        	}
        }        
        @rmdir($f);
    }
    return $deleted;
}

function deltreeatime($f,$days) {
	$deleted=0;
	$today = date("Ymd",time() - ($days*60*60*24));
    if (is_dir($f)) {
        foreach(glob($f.'/*') as $sf) {
            if (is_dir($sf) && !is_link($sf)) {
                $deleted += deltreeatime($sf,$days);
            } else {
                $lastm = date("Ymd",fileatime($sf));
                if ($lastm<=$today) { 
                	@unlink($sf);
                	$deleted+=1;
                }
            }
        }
        @rmdir($f);
    }
    return $deleted;
}

function createDir($dirname) {
	global $_log;
	if (!mkdir($dirname,0777,true)) {
		$_log->error("Unable to create directory $dirname");
	} else {
		if (!chmod($dirname,0777)) {
			$_log->error("Unable to chmod directory $dirname");
		}
	}
}

/**
 * Return human readable sizes
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.1.0
 * @link        http://aidanlister.com/repos/v/function.size_readable.php
 * @param       int    $size        Size
 * @param       int    $unit        The maximum unit
 * @param       int    $retstring   The return string format
 * @param       int    $si          Whether to use SI prefixes
 */
function size_readable($size, $unit = null, $retstring = null, $si = true)
{
    // Units
    if ($si === true) {
        $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
        $mod   = 1000;
    } else {
        $sizes = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
        $mod   = 1024;
    }
    $ii = count($sizes) - 1;
 
    // Max unit
    $unit = array_search((string) $unit, $sizes);
    if ($unit === null || $unit === false) {
        $unit = $ii;
    }
 
    // Return string
    if ($retstring === null) {
        $retstring = '%01.2f %s';
    }
 
    // Loop
    $i = 0;
    while ($unit != $i && $size >= 1024 && $i < $ii) {
        $size /= $mod;
        $i++;
    }
 
    return sprintf($retstring, $size, $sizes[$i]);
}

?>